package com.bw.usercenter.ui.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.bw.mvpcore.view.BaseMVPFragment
import com.bw.usercenter.R
import com.bw.usercenter.adapter.MyItemAdapter
import com.bw.usercenter.adapter.MyRecommentAdapter
import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.resp.RespGoodEntity
import com.bw.usercenter.model.protocol.resp.RespRecommendEntity
import com.bw.usercenter.model.protocol.resp.RespSearchEntity
import com.bw.usercenter.model.protocol.resp.RespSearchitemIDEntity
import com.bw.usercenter.presenter.UserCenterPresenterImpl
import com.bw.usercenter.ui.mainview.FindActivity
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.loader.ImageLoader
import kotlinx.android.synthetic.main.fragment_home.*


class HomeFragment : BaseMVPFragment<UserCenterPresenterImpl>(),UserCenterContract.UserCenterView {
    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun ClearStatBar() {

    }

    override fun initEvent() {
        home_recyclerview.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        home_recommerRecyclerView.layoutManager = StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.VERTICAL)
        mPresenter.requestTB(1)
        mPresenter.getRecomment()
        Mybanner()
/*        home_findd.isFocusableInTouchMode = false
        home_findd.setOnClickListener {
            home_findd.isFocusableInTouchMode = true
        }*/
        home_findd.setOnClickListener {
            startActivity(Intent(context,FindActivity::class.java))
            activity?.overridePendingTransition(R.anim.left_enter_anim,R.anim.left_exit_anim)
        }
//        home_findd.setOnFocusChangeListener(object : View.OnFocusChangeListener{
//            override fun onFocusChange(v: View?, hasFocus: Boolean) {
//                if (hasFocus){
//
//                }else{
//
//                }
//            }
//        })


    }

    fun Mybanner(){
        var bannerlist:ArrayList<String> = arrayListOf()
        bannerlist.add("http://p3.qhimg.com/bdm/1366_768_85/t012fee75187b20ea60.jpg")
        bannerlist.add("http://p8.qhimg.com/bdm/1280_800_85/t011b329f2e0b5232a8.jpg")
        bannerlist.add("http://p5.qhimg.com/bdm/1280_800_85/t019796d76712a60d1c.jpg")
        bannerlist.add("http://p4.qhimg.com/bdm/1024_768_85/t01f6daa9885d147773.jpg")
        bannerlist.add("http://p9.qhimg.com/bdm/1440_900_85/t016fdb7fe821d106c1.jpg")
        //轮播样式
        home_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
        //加载图片
        home_banner.setImageLoader(MyLoder())
        //设置图片集合
        home_banner.setImages(bannerlist)
        //轮播动画
        home_banner.setBannerAnimation(Transformer.Default)
        //自动轮播
        home_banner.isAutoPlay(true)
        home_banner.start()
    }

    class MyLoder :ImageLoader() {
        override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
            Glide.with(context!!).load(path as String).into(imageView!!)
        }

    }

    override fun initData(savedInstanceState: Bundle?) {

    }
    override fun createPresenter(): UserCenterPresenterImpl {
        return UserCenterPresenterImpl(this@HomeFragment)
    }


    override fun releaseResource() {

    }

    override fun <T> registerSuccess(data: T) {
        var respgood: RespGoodEntity = data as RespGoodEntity
        var  data : List<RespGoodEntity.Data>? = respgood.data
        var myItemAdapter:MyItemAdapter = MyItemAdapter(data as MutableList<RespGoodEntity.Data>?)
        home_recyclerview.adapter = myItemAdapter
    }

    override fun registerSuccess(data: RespRecommendEntity) {
        var recommlist:MutableList<RespRecommendEntity.Data> = data.data as MutableList<RespRecommendEntity.Data>
        var myRecommentAdapter:MyRecommentAdapter = MyRecommentAdapter(recommlist)
        home_recommerRecyclerView.adapter = myRecommentAdapter
    }

    override fun registerFaild(throwable: Throwable) {

    }

    override fun registerSrech(respSearchEntity: RespSearchEntity) {


    }

    override fun getSearchitem(respSearchitemIDEntity: RespSearchitemIDEntity) {

    }

}